<template>
  <div class="login">
    <div class="login-weaper">
      <div class="login-left">
        <div class="login-time"></div>
        <div class="login-left-box">
          <div>
            <h2 class="title">儿童食品系统</h2>
            <div class="msg">
              <div class="msg-author">
                <span>欢迎您的到来 </span>
                <span> </span>
              </div>
              <div class="msg-txt">
                这里是管理系统简单介绍 此系统主要依据孩子在 不同年龄段所 需的营养元素 而给父母推出的 蔬菜、水果等食品的介绍和做法
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-main">
          <h4 class="login-title">用户登录</h4>
          <el-form
              class="el-form login-form"
              :model="ruleForm"
              :rules="rules"
              ref="formName"
              @keyup.enter.native="submitForm"
          >
            <el-form-item style="margin-left: 0px" prop="username">
              <el-input
                  type="text"
                  placeholder="请输入账号"
                  prefix-icon="el-icon-user"
                  v-model="ruleForm.username"
                  clearable
                  autocomplete="off"
              >
              </el-input>
            </el-form-item>
            <el-form-item style="margin-left: 0px" prop="password">
              <el-input
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="el-icon-lock"
                  v-model="ruleForm.password"
                  clearable
                  autocomplete="off"
                  :show-password="true"
              >
              </el-input>
            </el-form-item>
            <el-form-item style="margin: 40px 0px 0">
              <Vcode :show="isShow" @success="success" @close="close" />
              <el-button
                  type="primary"
                  class="login-submit"
                  @click="submitForm"
              >
                <span>登录</span>
              </el-button>
              <a class="link_1" @click="$router.push('/mother/Regist')">没有账号？点我注册</a>
            </el-form-item>
            <div>
            </div>
          </el-form>
          <div class="login-menu">
            <a href="javascript:;"> </a>
            <a href="javascript:;"> </a>
          </div>
        </div>
      </div>
    </div>
    <div class="vue-particles">
      <vue-particles
          color="#dedede"
          shapeType="star"
          linesColor="#dedede"
          hoverMode="grab"
          clickMode="push"
          style="height: 100%"
      ></vue-particles>
    </div>
  </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";
import request from "@/router/request";
export default {
  name: "Login",
  components: {
    Vcode
  },
  data() {
    return {
      isShow: false, // 验证码模态框是否出现
      submit: {
        loading: false,
      },
      ruleForm: {
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //   登录
    submitForm() {
      this.$refs["formName"].validate((valid) => {
        if (valid) {
          this.isShow = true;//让验证框出现
        }
      });
    },
    // 用户通过了验证
    success() {
      this.isShow = false;// 通过验证后，需要手动隐藏模态框
      request.post("/user/login",this.ruleForm).then(res=>{
        if (res.state ==='0'){
          console.log(res)
          this.$message.success("登录成功");
          sessionStorage.setItem("user1",JSON.stringify(res.data))//存储用户信息
          this.$router.push("/mother");
        }else if (res.state==='-1'){
          this.$message.error(res.message)
        }else {
          this.$message.error("登录时发生未知的异常")
        }
      })
    },
    // 用户点击遮罩层，应该关闭模态框
    close() {
      this.isShow = false;
    },
  }
}
</script>

<style scoped lang="scss">
.login {
  height: 100%;
  width: 100%;
  overflow: hidden;
  display: flex;
  position: relative;
  .vue-particles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(
            ellipse at top left,
            rgba(105, 155, 200, 1) 0%,
            rgba(181, 197, 216, 1) 57%
    );
  }
  .login-weaper {
    margin: auto;
    height: 500px;
    display: flex;
    box-sizing: border-box;
    position: relative;
    z-index: 1;
    border: none;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    .link_1:hover{
      color:#00a4ff;
    }
    .login-left {
      width: 491px;
      padding: 20px;
      font-size: 16px;
      color: #fff;
      position: relative;
      background-color: rgba(64, 158, 255, 0.8);
      display: flex;
      flex-direction: column;
      border-top-left-radius: 4px;
      border-bottom-left-radius: 4px;
      .login-time {
        width: 100%;
        color: #fff;
        opacity: 0.9;
        font-size: 14px;
        overflow: hidden;
      }
      .login-left-box {
        flex: 1;
        overflow: hidden;
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        padding: 80px 45px;
        .logo {
          font-size: 22px;
          margin-bottom: 15px;
        }
        .title {
          color: #fff;
          font-weight: 300;
          letter-spacing: 2px;
          font-size: 16px;
        }
        .msg {
          color: #fff;
          font-size: 13px;
          margin-top: 35px;
          .msg-author {
            opacity: 0.6;
            span:last-of-type {
              margin-left: 15px;
            }
          }
          .msg-author1 {
            opacity: 0.6;
            span:last-of-type {
              margin-left: 15px;
            }
          }
          .msg-txt {
            margin-top: 15px;
            line-height: 22px;
          }
        }
      }
    }
    .login-right {
      width: 491px;
      padding: 20px;
      position: relative;
      align-items: center;
      display: flex;
      background-color: rgba(255, 255, 255, 1);
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      .login-main {
        margin: 0 auto;
        width: 70%;
        .login-title {
          color: #333;
          margin-bottom: 40px;
          font-weight: 500;
          font-size: 22px;
          text-align: center;
          letter-spacing: 4px;
        }
        .login-form {
          margin: 10px 0;
          i {
            color: #333;
          }
          .el-form-item {
            margin-bottom: 20px !important;
            .login-code {
              display: flex;
              align-items: center;
              justify-content: space-around;
              margin: 0 0 0 10px;
              user-select: none;
              .login-code-img {
                margin-top: 2px;
                width: 100px;
                height: 38px;
                border: 1px solid #dcdfe6;
                color: #333;
                font-size: 14px;
                font-weight: 700;
                letter-spacing: 5px;
                line-height: 38px;
                text-indent: 5px;
                text-align: center;
                cursor: pointer;
                transition: all ease 0.2s;
                border-radius: 4px;
                &:hover {
                  border-color: #c0c4cc;
                  transition: all ease 0.2s;
                }
              }
            }
            .login-submit {
              width: 100%;
              height: 45px;
              letter-spacing: 2px;
              font-weight: 300;
            }
          }
        }
        .login-menu {
          margin-top: 30px;
          width: 100%;
          text-align: left;
          a {
            color: #999;
            font-size: 12px;
            margin: 0 8px;
            text-decoration: none;
            &:hover {
              color: #1e9fff;
              text-decoration: underline;
            }
          }
        }
      }
    }
  }
}
</style>
